<?php


namespace seecms\lib\builder\control;

use seecms\See;

class TextareaControl extends Control
{
    protected $type = 'textarea';

    protected $edit = false;

    /**
     * 磁盘
     * @var string
     */
    protected $disk = '';

    /**
     * 提交路径
     * @var string
     */
    protected $url = '';

    public function isEdit(): bool
    {
        return $this->edit;
    }

    /**
     * @param bool $edit
     * @return $this
     */
    public function setEdit(bool $edit = true): TextareaControl
    {
        $this->edit = $edit;

        return $this;
    }

    public function getDisk(): string
    {
        return $this->disk ?: '';
    }

    /**
     * @param string $disk
     * @return $this
     */
    public function setDisk(string $disk): TextareaControl
    {
        $this->disk = $disk;

        return $this;
    }

    public function getUrl(): string
    {
        return $this->url ?: See::config()->upload->url();
    }

    public function setUrl(string $url): TextareaControl
    {
        $this->url = $url;

        return $this;
    }

    public function render(): string
    {
        $required = $this->required ? '<span class="layui-font-red">*</span>' : '';

        $readonly = $this->readonly ? 'readonly' : '';
        $disabled = $this->disabled ? 'disabled' : '';
        $description = $this->getDescription();

        $edit = '';
        if ($this->edit) {
            $edit = <<<EOF
                    textarea-fetch-rich
                      id="editor-{$this->name}"
                      data-url="{$this->getUrl()}"
EOF;
        }

        $display = 'block';
        $container = '<div class="layui-form-item">';
        if ($this->inline == 'inline') {
            $display = 'inline';
            $container = '<div class="layui-inline">';
        }

        return <<<EOF
$container
    <label class="layui-form-label">
        {$this->label}
        {$required}
    </label>
    <div class="layui-input-$display">
        <textarea name="{$this->name}"
                  class="layui-textarea"
                  placeholder="{$this->placeholder}"
                  $edit
                  $readonly
                  $disabled>{$this->value}</textarea>
        $description
    </div>
</div>
EOF;

    }
}